package cn.jason.org.service.impl;

import cn.jason.basic.exception.BusinessException;
import cn.jason.basic.service.impl.BaseServiceImpl;
import cn.jason.basic.util.BaiduAuditUtils;
import cn.jason.org.domain.Employee;
import cn.jason.org.domain.Shop;
import cn.jason.org.domain.ShopAuditLog;
import cn.jason.org.mapper.EmployeeMapper;
import cn.jason.org.mapper.ShopAuditLogMapper;
import cn.jason.org.mapper.ShopMapper;
import cn.jason.org.service.IShopService;
import cn.jason.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jason
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {


    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Override
    public void settelment(Shop shop) {
        Employee admin = shop.getAdmin();

        //参数空校验
        if(StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getAddress())
                || StringUtils.isEmpty(admin.getUsername())){
            throw new BusinessException("参数不能为空");
        }
        //合法校验
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("内容违规,请重新编写！");
        }
        //判断是否已经注册过
        Shop shopTmp = shopMapper.loadByName(shop.getName());
        //店铺不能被注册
        if(shopTmp != null){
            throw new BusinessException("店铺已经被注册，如果忘记密码找回密码！！！");
        }

        //人是否被注册过
        Employee employee = employeeMapper.loadByUsername(admin.getUsername());

        if(employee != null && employee.getShop_id() != null ){
            throw new BusinessException("该用户已注册或者已有绑定店铺！！");
        }
        //注册但是没有绑定店铺
        if (employee==null){
            employeeMapper.save(admin);
            employee=admin;
        }
        //保存数据
        shop.setAdminId(employee.getId());
        shopMapper.save(shop);

        //反过来修改shop中的admin_id
        employee.setShop_id(shop.getId());
        employeeMapper.update(employee);




    }

    @Override
    public void pass(ShopAuditLog log) {
        //修改店铺状态 审核通过
        System.out.println(log);
        Shop shop = shopMapper.loadById(log.getShopId());
        shop.setState(1);
        shopMapper.update(shop);

        //记录审核通过
        log.setAuditId(333L);
        shopAuditLogMapper.save(log);

        //发送通过激活邮件
        /*Employee employee = employeeMapper.loadById(log.getShopId());
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("294059913@qq.com");
        message.setTo("294059913@qq.com");
        message.setSubject("宠物乐园-审核通过激活连接");
        message.setText("你的审核信息已通过，请尽快填写激活信息"+"激活链接：http://localhost:8080/shop/activation/"
                +log.getShopId());
        javaMailSender.send(message);*/
    }

    @Override
    public void reject(ShopAuditLog log) {
        //修改店铺状态  审核驳回
        Shop shop = shopMapper.loadById(log.getShopId());
        shop.setState(-1);
        shopMapper.update(shop);

        //记录审核未通过日志
        log.setAuditId(333L);
        shopAuditLogMapper.save(log);

        //发送驳回邮件链接
        /*Employee employee = employeeMapper.loadByShopId(log.getShopId());
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("294059913@qq.com");
        message.setTo("294059913@qq.com");
        message.setSubject("宠物乐园-店铺入驻驳回");
        message.setText("您的店铺信息审核不通过,请重新上传" + "http://localhost:8081/#/register2?shopId="+log.getShopId());
        javaMailSender.send(message);*/

    }

    @Override
    public List<ShopVo> queryEcharts() {
        return shopMapper.queryEcharts();
    }
}
